package com.atguigu.gulimail.search.thread;

import java.util.concurrent.*;

/**
 * @author fangy
 * @date 2022-10-03 20:10
 */
public class ThreadTest {

    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public  void yes(String[] args) throws ExecutionException, InterruptedException {

//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getId());
//            int i = 10 / 0;
//            System.out.println("运行结果:" + i);
//            return i;
//        }, executor).whenComplete((res, exce) -> {
//            System.out.println("返回的结果为" + res + "异常为" + exce);
//        }).exceptionally((exec) -> {
//            return 10;
//        });

        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果:" + i);
            return i;
        }, executor).handle((res,throwable)->{
            if(res != null){
                return res *= 2;
            }
            if(throwable != null){
                return 0;
            }
            return 0;
        });

        System.out.println("main......end........"+future.get());

//        new ThreadPoolExecutor();

    }

    public static void main(String[] args) {

        new thread01().start();
        System.out.println("main");

        thread02 thread02 = new thread02();
        new Thread(thread02).start();

        thread03 thread03 = new thread03();
        FutureTask futureTask = new FutureTask<>(thread03);

        new Thread(futureTask).start();


        System.out.println("main...end");
    }

    public static class thread01 extends Thread {

        @Override
        public void run() {
            try {
                Thread.sleep(1000);
                System.out.println("mythread");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    private static class thread02 implements Runnable {

        @Override
        public void run() {
            try {
                Thread.sleep(1000);
                System.out.println("mythread");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private static class thread03 implements Callable {


        @Override
        public Object call() throws Exception {
            try {
                Thread.sleep(1000);
                int i = 10/6;
                System.out.println("mythread");
                return i;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 0;
        }
    }


}
